/*
 * @Descripttion:路由工具类方法
 * @LastEditors: xzh
 * @LastEditTime: 2023-08-21 10:29:04
 */
import { object_Inf } from "@/utils/typings/index.type";
import Layout from "@/layout/index.vue";
import Dile_Constants from "@/utils/constants/app";
import { addStaticRouter_Inf } from "@/utils/constants/app/index.d";
import router from "@/router";
import type { routerHistory_Inf } from "@/store/router/index.type";
import Dile_StoreAction from "@/utils/cache/store";
import Format_Methods from "../format";

/*
 * @Descripttion:通用工具方法类
 * @LastEditors: xzh
 * @LastEditTime: 2022-05-25 20:21:52
 */
export default class Router_Methods extends Format_Methods {
  /**
   * @Author: xzh
   * @Descripttion:获取当前路由缓存参数
   * @Param:
   */
  static getRouterMeta = () => Dile_StoreAction.getRouterMeta();
  /**
   * @Author: xzh
   * @Descripttion:添加静态路由
   * @Param:
   * @param {addStaticRouter_Inf} addRouter-添加路由对象
   */
  static addStaticRouter = async (addRouter: addStaticRouter_Inf) => {
    let { query } = addRouter;
    //记录路由参数缓存
    let routerKey = this.recordStoreRouterMeta(query || {});
    //格式化路由格式
    let routeItem = this.formatRouter(routerKey, addRouter);
    //添加&&跳转路由
    router.addRoute(routeItem);
    //获取格式化后path
    let { name }: object_Inf = routeItem.children.find(() => true) || {};
    //跳转路由
    Dile_StoreAction.toggleRouter({
      props: {
        name,
      },
    });
  };

  /**
   * @Author: xzh
   * @Descripttion:格式化路由格式
   * @Param:
   * @param {string} routerKey-路由key
   * @param {addStaticRouter_Inf} addRouter-添加路由对象
   */
  static formatRouter(routerKey: string, addRouter: addStaticRouter_Inf) {
    let { title, realPath } = addRouter;
    return {
      path: "/static",
      component: Layout,
      children: [
        {
          path: "/static/" + routerKey,
          name: routerKey,
          component:
            Dile_Constants.modulesMap[
              /* @vite-ignore */
              `${Dile_Constants.matchPrefix}/${realPath}.vue`
            ],
          meta: {
            title,
            guid: routerKey,
            realPath,
          },
        },
      ],
    };
  }
  /**
   * @Author: xzh
   * @Descripttion:记录路由参数缓存
   * @Param:
   * @param {object_Inf} query
   */
  static recordStoreRouterMeta(query: object_Inf) {
    let routerKey = super.newGuid();
    //添加路由参数缓存
    Object.keys(query).length > 0 &&
      Dile_StoreAction.setRouterMetaMap({
        guid: routerKey,
        meta: query,
      });
    return routerKey;
  }
  /**
   * @Author: xzh
   * @Descripttion:拼接静态路由历史
   * @Param:
   */
  static jointStaticRouterHistory() {
    let routerHistoryArr = Dile_StoreAction.routerHistoryArr();
    //获取路由历史中静态路由-name为guid格式
    return (
      routerHistoryArr
        .filter((x) => this.isGuidFormat(x.name))
        .map((routerHistory: routerHistory_Inf) => {
          let { title, realPath, guid } = routerHistory;
          return this.formatRouter(guid, { title, realPath });
        }) || [].flat()
    );
  }
  // static formatRouterArr(guid: string) {
  //   //路由菜单结构
  //   let routerMenuArr: Array<resData_Inf> = [];
  //   // 查询根节点
  //   let routerData = Dile_Constants.routerArr.find((x) => x.guid == guid);

  //   rootRouterArr.forEach((routerData) => {
  //     let { liDataName, liName, liIcon, guid } = routerData;
  //     let router: routerMenu_Inf = {
  //       path: "/" + liDataName,
  //       name: liDataName,
  //       children: [],
  //       component: Layout,
  //       meta: {
  //         title: liName,
  //         guid,
  //         icon: liIcon,
  //       },
  //     };
  //   });
  // }
  // joinChildRouterArr(routerData: resData_Inf) {
  //   let { guid } = routerData;
  //   //获取对应子路由
  //   let children = Dile_Constants.routerArr.filter(
  //     (x) => x.parentGuid == guid && x.isEnable != false
  //   );

  //   children.forEach((childRouter) => {
  //     let { liDataName, liName, liIcon, guid } = routerData;
  //     childRouter.children = this.joinChildRouterArr(childRouter);
  //   });

  //   return children;
  // }
}
